<?php
namespace app\admin\controller;
use app\service\propertyService;
use think\facade\Db;
use think\facade\View;

class Property extends Common{
    public function propertyList(){
        //查询服务商数据条数
        $com = $this->pidTokenId();
        //$where['id'] = array('in',$com['property_id']);
        $where[] = ['id','in',$com['property_id']];
        //查询服务商信息
        $data = request()->get();
        if (!empty($data)){
            if(!empty($data['property_name'])){
                //$where['name'] = array("like","%".$data['property_name']."%");//账单编号
                $where[] = ['name','like',"%".$data['property_name']."%"];
            }
        }
        $where = array_filter($where);
        $prolist = (new propertyService())->propertyList($where);
        $number = (new propertyService())->propertyNumber($where);
        View::assign('number',$number);
        if(!empty($number)){
            foreach($prolist as $lis){
                $id = $lis['service_id'];
                $proname = (new propertyService())->propertyIdName($id);
                $lis['service_id'] = $proname['name'];
                $list[] = $lis;
                View::assign('list',$list);
            }
        }else{
            View::assign('list',$prolist);
        }
        $page=$prolist->render();
        View::assign('prolist',$prolist);
        View::assign('page',$page);
        foreach ($data as $k => $v) {
            View::assign($k, $v);
        }

        return View::fetch('property_list');
    }
    public function propertySet(){

        //查询服务商数据条数
        $com = $this->pidTokenId();

        $where[] = ['id','in',$com['property_id']];
        //查询服务商信息
        $data = request()->get();
        if (!empty($data)){
            if(!empty($data['property_name'])){
                $where[] = ['name','like',"%".$data['property_name']."%"];
            }else{
                $where['name'] = "";
            }
        }
        $where = array_filter($where);
        $prolist = (new propertyService())->propertyList($where);
        $number = (new propertyService())->propertyNumber($where);
        View::assign('number',$number);
        if(!empty($number)){
            foreach($prolist as $lis){
                $id = $lis['service_id'];
                $proname = (new propertyService())->propertyIdName($id);
                $lis['service_id'] = $proname['name'];
                $list[] = $lis;
                View::assign('list',$list);
            }
        }else{
            View::assign('list',$prolist);
        }
        View::assign('prolist',$prolist);
        foreach ($data as $k => $v) {
            View::assign($k, $v);
        }
        
        return View::fetch('property_set');
    }
    //物业添加
    public function propertyAdd(){
        //查询服务商信息
        $com = $this->pidTokenId();
        $where['id'] = array('in',$com['service_id']);
        $servicelist = (new propertyService())->serviceList($where);
        View::assign('servicelist',$servicelist);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //判断账号名称是否重复        
            $info = (new propertyService())->propertyName($data['name']);
            if($info){
                $this->error('物业已存在');
            }
            $data['time'] = date('Y-m-d H:i:s',time());
            //插入数据到数据库
            $result = (new propertyService())->propertyAdd($data);
            if($result){
                return "添加物业成功";
                $this->success('添加物业成功',url('propertyList'));
            }else{
                $this->error('添加物业失败',url('propertyAdd'));
            }
        }
        return View::fetch('property_add');
    }
    //物业修改
    public function propertyEdit(){
        //查询服务商信息
        $com = $this->pidTokenId();
        $where['id'] = array('in',$com['service_id']);
        $servicelist = (new propertyService())->serviceList($where);
        View::assign('servicelist',$servicelist);
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('物业不存在');
            }
            $info = (new propertyService())->propertyId($id);
            View::assign('info',$info);
            return View::fetch('property_edit');
        }else{
            $data = request()->post();
            // dump($data);exit();
            $result = (new propertyService())->propertyUpdate($data);
            if($result){
                return "添加物业成功";
                $this->success('修改物业成功',url('propertyList'));
            }else{
                $this->error('修改物业失败');
            }
        }
        return View::fetch('property_edit');
    }
    //物业删除
    public function propertyDel(){
        $id = request()->param('id');
		$validate = $this->validate(request()->param(),[
			'id'	=>	'require|number'
		]);
		if($validate !== true){
			$this->error($validate);
        }
        //判断是否存在该数据
		$info = (new propertyService())->propertyId($id);
		if(empty($info)){
			$this->error('该物业不存在');
		}
		$result = Db::name('property')->delete($id);
        if($result){

            $this->success('删除物业成功',url('propertyList'));
		}else{
            $this->error('删除物业失败',url('propertyList'));
		}
    }

    //添加微信信息
    public function weixin(){
        if(request()->isGet()){
            $id = request()->param('id');
            $validate = $this->validate(request()->param(),[
                'id'	=>	'require|number'
            ]);
            if($validate !== true){
                $this->error($validate);
            }
            $info = (new propertyService())->propertyId($id);
            View::assign('info',$info);

            if($info){
                return View::fetch('property_weixin');
            }else{
                $this->error('物业信息错误',url('propertyList'));
            }
        }else{
            //获取表单数据
            $data = request()->post();
            //将数据插入数据库
            $result = (new propertyService())->propertyUpdate($data);
            //判断数据是否插入成功
            if($result){
                $this->success('配置微信信息成功',url('propertyList'));
            }else{
                $this->error('配置微信信息失败',url('propertyList'));
            }
        }
    }

    //添加发票信息
    public function invoice(){
        if(request()->isGet()){
            $id = request()->param('id');
            $validate = $this->validate(request()->param(),[
                'id'	=>	'require|number'
            ]);
            if($validate !== true){
                $this->error($validate);
            }
            $info = (new propertyService())->propertyId($id);
            View::assign('info',$info);
            if($info){
                return View::fetch('property_invoice');
            }else{
                $this->error('物业信息错误',url('propertyList'));
            }
        }else{
            //获取表单数据
            $data = request()->post();
            //将数据插入数据库
            $result = (new propertyService())->propertyUpdate($data);
            //判断数据是否插入成功
            if($result){
                $this->success('配置发票信息成功',url('propertyList'));
            }else{
                $this->error('配置发票信息失败',url('propertyList'));
            }
        }
    }
}